package com.example.vault.config;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * 多数据源配置类
 * 支持交易库、用户库、日志库的主从数据库配置
 * 从Vault动态获取数据库连接凭据
 */
@Configuration
public class MultiDataSourceConfig {

    // ==================== 交易库配置 ====================
    
    @Value("${datasource.transaction.master.url}")
    private String transactionMasterUrl;
    
    @Value("${datasource.transaction.master.username}")
    private String transactionMasterUsername;
    
    @Value("${datasource.transaction.master.password}")
    private String transactionMasterPassword;
    
    @Value("${datasource.transaction.slave.url}")
    private String transactionSlaveUrl;
    
    @Value("${datasource.transaction.slave.username}")
    private String transactionSlaveUsername;
    
    @Value("${datasource.transaction.slave.password}")
    private String transactionSlavePassword;

    /**
     * 交易库主库数据源
     */
    @Bean(name = "transactionMasterDataSource")
    @Primary
    public DataSource transactionMasterDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl(transactionMasterUrl);
        dataSource.setUsername(transactionMasterUsername);
        dataSource.setPassword(transactionMasterPassword);
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        
        // 连接池配置
        dataSource.setMaximumPoolSize(20);
        dataSource.setMinimumIdle(5);
        dataSource.setConnectionTimeout(30000);
        dataSource.setIdleTimeout(600000);
        dataSource.setMaxLifetime(1800000);
        dataSource.setLeakDetectionThreshold(60000);
        
        return dataSource;
    }

    /**
     * 交易库从库数据源
     */
    @Bean(name = "transactionSlaveDataSource")
    public DataSource transactionSlaveDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl(transactionSlaveUrl);
        dataSource.setUsername(transactionSlaveUsername);
        dataSource.setPassword(transactionSlavePassword);
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        
        // 从库连接池配置（读操作较多）
        dataSource.setMaximumPoolSize(30);
        dataSource.setMinimumIdle(10);
        dataSource.setConnectionTimeout(30000);
        dataSource.setIdleTimeout(600000);
        dataSource.setMaxLifetime(1800000);
        
        return dataSource;
    }

    /**
     * 交易库JdbcTemplate
     */
    @Bean(name = "transactionJdbcTemplate")
    @Primary
    public JdbcTemplate transactionJdbcTemplate(@Qualifier("transactionMasterDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    /**
     * 交易库事务管理器
     */
    @Bean(name = "transactionTransactionManager")
    @Primary
    public PlatformTransactionManager transactionTransactionManager(@Qualifier("transactionMasterDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    // ==================== 用户库配置 ====================
    
    @Value("${datasource.user.master.url}")
    private String userMasterUrl;
    
    @Value("${datasource.user.master.username}")
    private String userMasterUsername;
    
    @Value("${datasource.user.master.password}")
    private String userMasterPassword;
    
    @Value("${datasource.user.slave.url}")
    private String userSlaveUrl;
    
    @Value("${datasource.user.slave.username}")
    private String userSlaveUsername;
    
    @Value("${datasource.user.slave.password}")
    private String userSlavePassword;

    /**
     * 用户库主库数据源
     */
    @Bean(name = "userMasterDataSource")
    public DataSource userMasterDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl(userMasterUrl);
        dataSource.setUsername(userMasterUsername);
        dataSource.setPassword(userMasterPassword);
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        
        // 用户库连接池配置
        dataSource.setMaximumPoolSize(15);
        dataSource.setMinimumIdle(3);
        dataSource.setConnectionTimeout(30000);
        dataSource.setIdleTimeout(600000);
        dataSource.setMaxLifetime(1800000);
        
        return dataSource;
    }

    /**
     * 用户库从库数据源
     */
    @Bean(name = "userSlaveDataSource")
    public DataSource userSlaveDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl(userSlaveUrl);
        dataSource.setUsername(userSlaveUsername);
        dataSource.setPassword(userSlavePassword);
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        
        dataSource.setMaximumPoolSize(20);
        dataSource.setMinimumIdle(5);
        dataSource.setConnectionTimeout(30000);
        dataSource.setIdleTimeout(600000);
        dataSource.setMaxLifetime(1800000);
        
        return dataSource;
    }

    /**
     * 用户库JdbcTemplate
     */
    @Bean(name = "userJdbcTemplate")
    public JdbcTemplate userJdbcTemplate(@Qualifier("userMasterDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    /**
     * 用户库事务管理器
     */
    @Bean(name = "userTransactionManager")
    public PlatformTransactionManager userTransactionManager(@Qualifier("userMasterDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    // ==================== 日志库配置 ====================
    
    @Value("${datasource.log.master.url}")
    private String logMasterUrl;
    
    @Value("${datasource.log.master.username}")
    private String logMasterUsername;
    
    @Value("${datasource.log.master.password}")
    private String logMasterPassword;
    
    @Value("${datasource.log.slave.url}")
    private String logSlaveUrl;
    
    @Value("${datasource.log.slave.username}")
    private String logSlaveUsername;
    
    @Value("${datasource.log.slave.password}")
    private String logSlavePassword;

    /**
     * 日志库主库数据源
     */
    @Bean(name = "logMasterDataSource")
    public DataSource logMasterDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl(logMasterUrl);
        dataSource.setUsername(logMasterUsername);
        dataSource.setPassword(logMasterPassword);
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        
        // 日志库连接池配置（写入较多）
        dataSource.setMaximumPoolSize(25);
        dataSource.setMinimumIdle(5);
        dataSource.setConnectionTimeout(30000);
        dataSource.setIdleTimeout(300000);
        dataSource.setMaxLifetime(1800000);
        
        return dataSource;
    }

    /**
     * 日志库从库数据源
     */
    @Bean(name = "logSlaveDataSource")
    public DataSource logSlaveDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl(logSlaveUrl);
        dataSource.setUsername(logSlaveUsername);
        dataSource.setPassword(logSlavePassword);
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        
        dataSource.setMaximumPoolSize(15);
        dataSource.setMinimumIdle(3);
        dataSource.setConnectionTimeout(30000);
        dataSource.setIdleTimeout(600000);
        dataSource.setMaxLifetime(1800000);
        
        return dataSource;
    }

    /**
     * 日志库JdbcTemplate
     */
    @Bean(name = "logJdbcTemplate")
    public JdbcTemplate logJdbcTemplate(@Qualifier("logMasterDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    /**
     * 日志库事务管理器
     */
    @Bean(name = "logTransactionManager")
    public PlatformTransactionManager logTransactionManager(@Qualifier("logMasterDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}